FUNCTION zsd_fm001.
*"----------------------------------------------------------------------
*"*"本地接口：
*"  TABLES
*"      ET_RETURN STRUCTURE  ZSDS002
*"      IT_DATA_IN STRUCTURE  ZSDS002
*"----------------------------------------------------------------------

  DATA :lv_x,
        ls_data_in       LIKE LINE OF it_data_in,

        g_partner        TYPE bu_partner,
        l_partn_cat      TYPE bapibus1006_head-partn_cat,          "业务伙伴类别
        l_partn_grp      TYPE bapibus1006_head-partn_grp,         "业务伙伴分组
        ls_central       TYPE bapibus1006_central,                 "一般 BP 数据，与业务伙伴类别无关
        ls_central_organ TYPE bapibus1006_central_organ,           "SAP BP: 组织数据的 BAPI 结构
        ls_address       TYPE bapibus1006_address,
        ls_address_x     TYPE  bapibus1006_address_x,
        l_bpartner       TYPE bapibus1006_head-bpartner, "业务伙伴编码

        s_tatyp          TYPE knvi-tatyp,
        s_taxkd          TYPE knvi-taxkd,

        lt_return        TYPE TABLE OF bapiret2,
        ls_return        TYPE   bapiret2,
        lt_return_tax    TYPE TABLE OF bapiret2,
        ls_return_tax    TYPE          bapiret2,

        lt_bapiadsmtx    TYPE TABLE OF bapiadsmtx,
        ls_bapiadsmtx    TYPE          bapiadsmtx,
        lt_bapiret2      TYPE TABLE OF bapiret2,
        ls_bapiret2      TYPE          bapiret2,
        lt_bapiadtel     LIKE TABLE OF bapiadtel,
        ls_bapiadtel     LIKE          bapiadtel,
        lt_bapicomrem    LIKE TABLE OF bapicomrem,
        ls_bapicomrem    LIKE          bapicomrem,
        lt_bapicomremx   LIKE TABLE OF bapicomrex,
        ls_bapicomremx   LIKE          bapicomrex,
        ls_kna1          TYPE          kna1,
        ls_knb1          TYPE          knb1,
        ls_knvv          TYPE knvv,
        lt_fknvp         LIKE TABLE OF fknvp,
        ls_fknvp         LIKE          fknvp,
        ls_knvp          TYPE knvp,
        g_kna1           TYPE kna1,
        lt_fknvi         LIKE TABLE OF fknvi,
        ls_fknvi         TYPE          fknvi.
  DATA: s_return LIKE LINE OF et_return.

  DATA:a(3).

  DATA:ls_mdata    TYPE cmds_ei_main,
       ls_header   TYPE cmds_ei_header,
       lt_customer TYPE cmds_ei_extern_t,
       ls_customer TYPE cmds_ei_extern,
       lt_company  TYPE cmds_ei_company_t,
       ls_company  TYPE cmds_ei_company,
       lt_tax      TYPE cmds_ei_tax_ind_t,
       ls_tax      TYPE cmds_ei_tax_ind,
       lt_function TYPE cmds_ei_functions_t,
       ls_function TYPE cmds_ei_functions,
       ls_cityc    TYPE cmds_ei_cmd_central,
       lt_sales    TYPE cmds_ei_sales_t,
       ls_sales    TYPE cmds_ei_sales,
       lt_texts    TYPE cvis_ei_text_t,
       ls_texts    TYPE cvis_ei_text,
       lt_retn     TYPE bapiret2_t,
       ls_retn     LIKE LINE OF lt_retn,

       lt_da       TYPE tline_tab,
       ls_da       TYPE LINE OF tline_tab.


  DATA:
    es_master_data_correct   TYPE cmds_ei_main,
    es_msg_correct           TYPE cvis_message,
    es_master_data_defective TYPE cmds_ei_main,
    es_msg_defective         TYPE cvis_message,

    lt_rete                  LIKE TABLE OF bapiret2,
    ls_rete                  LIKE          bapiret2.

  DATA: ls_bank  TYPE bapibus1006_bankdetail,
        ls_bankx TYPE bapibus1006_bankdetail_x,
        lt_re    TYPE TABLE OF bapiret2,
        ls_re    LIKE          bapiret2.

  DATA: ls_centraldata    TYPE bapibus1006_central,
        ls_centraldata_x  TYPE bapibus1006_central_x,
        ls_organization   TYPE bapibus1006_central_organ,
        ls_organization_x TYPE bapibus1006_central_organ_x.

  DATA: ls_bapiadtelx TYPE bapiadtelx,
        lt_bapiadtelx LIKE TABLE OF bapiadtelx,
        ls_bapicomrex TYPE bapicomrex,
        lt_bapicomrex LIKE TABLE OF bapicomrex.


*根据销售组织到表TVKO中取得公司代码
  SELECT tvkbz~vkbur,tvko~vkorg,tvko~bukrs INTO TABLE @DATA(lt_bukrs)
        FROM tvko INNER JOIN tvkbz ON tvkbz~vkorg = tvko~vkorg
        FOR ALL ENTRIES IN @it_data_in
        WHERE tvkbz~vkbur = @it_data_in-vkbur.

  SELECT partner,bpext
  INTO TABLE @DATA(lt_but000)
        FROM but000
        FOR ALL ENTRIES IN @it_data_in
        WHERE bpext = @it_data_in-bpext.

  SORT lt_bukrs  BY vkbur.
  SORT lt_but000 BY bpext.
  LOOP AT it_data_in INTO ls_data_in .

    IF ls_data_in-zstatu = 'S' AND ls_data_in-zmessg IS INITIAL .

      IF  ls_data_in-bu_group IS NOT INITIAL.
        CLEAR:lt_return,lt_bapicomrem,lt_bapiadtel.

        "V004 V003 外部给号
        IF ls_data_in-bu_group = 'V004'
        OR ls_data_in-bu_group = 'V003'.
          g_partner = ls_data_in-partner.
        ELSE.
          ls_data_in-partner = ''.
        ENDIF.

        ls_central-searchterm1 = ls_data_in-bu_sort1.  "搜索词*
        ls_address-postl_cod1 = ls_data_in-post_code1.     "邮政编码*
        ls_address-city = ls_data_in-city1.           "城市*
        ls_address-country = ls_data_in-country.        "国家*
        ls_address-region = ls_data_in-region.          "地区
        ls_address-langu = sy-langu.                   "语言*
        ls_central-partnerexternal = ls_data_in-bpext.  "外部业务伙伴编号
        ls_central_organ-name1 = ls_data_in-name1.  "组织名称*
        l_partn_cat = '2'.

        CLEAR:ls_bapiadtel.
        ls_bapiadtel-country = ls_data_in-country.
        ls_bapiadtel-telephone = ls_data_in-tel_number.    "客户联系电话
        APPEND ls_bapiadtel TO lt_bapiadtel.

        CLEAR: ls_bapicomrem.
        ls_bapicomrem-comm_type = 'TEL'.
        ls_bapicomrem-comm_notes = ls_data_in-remark.
        ls_bapicomrem-langu = '1'.
        ls_bapicomrem-consnumber =  001.
        APPEND ls_bapicomrem TO lt_bapicomrem.

        CALL FUNCTION 'BAPI_BUPA_CREATE_FROM_DATA'
          EXPORTING
            businesspartnerextern   = g_partner
            partnercategory         = l_partn_cat
            partnergroup            = ls_data_in-bu_group
            centraldata             = ls_central
            centraldataorganization = ls_central_organ
            addressdata             = ls_address
          IMPORTING
            businesspartner         = l_bpartner
          TABLES
            telefondata             = lt_bapiadtel
            communicationnotes      = lt_bapicomrem
            return                  = lt_return.
        CLEAR:lv_x.

        LOOP AT lt_return INTO ls_return WHERE type CA 'AIE'.
          lv_x = 'X'.
          s_return-zmessg = s_return-zmessg && '||' && ls_return-message.
        ENDLOOP.

        IF lv_x IS INITIAL.

          IF ls_data_in-partner IS INITIAL.
            ls_data_in-partner = l_bpartner.
            g_partner = ls_data_in-partner.
          ENDIF.
**
          CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
            EXPORTING
              wait = 'X'.
        ELSE.

          CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
        ENDIF.
        


*****添加银行*******         通用数据-支付交易
        ls_bank-bank_ctry = 'CN'. "国家代码
        ls_bank-bank_key = ls_data_in-bankl. "银行代码
        ls_bank-bank_acct = ls_data_in-bankn(18).


        CALL FUNCTION 'BAPI_BUPA_BANKDETAIL_ADD'
          EXPORTING
            businesspartner = ls_data_in-partner
            bankdetailid    = '0001'
            bankdetaildata  = ls_bank
*         IMPORTING
*           BANKDETAILIDOUT =
          TABLES
            return          = lt_re.

        CLEAR:lv_x.
        LOOP AT lt_re INTO ls_re WHERE type CA 'AIE'.
          lv_x = 'X'.
          s_return-zmessg = '客户主数据创建成功，银行数据错误' && s_return-zmessg && '||' && ls_re-message.
        ENDLOOP.

        IF lv_x IS NOT INITIAL.
          CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
        ELSE.
          CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
            EXPORTING
              wait = 'X'.
        ENDIF.
        CLEAR :ls_kna1,lt_re.
      ENDIF.


      "财务视图   FLCU00
      CALL FUNCTION 'BAPI_BUPA_ROLE_ADD_2'     "财务视图-公司代码
        EXPORTING
          businesspartner             = ls_data_in-partner
          businesspartnerrolecategory = 'FLCU01'    "BP 角色类别  BU_PARTNERROLECAT
          businesspartnerrole         = 'FLCU01'    "业务伙伴角色 BU_PARTNERROLE
        TABLES
          return                      = lt_bapiret2.

      CLEAR:lv_x.
      LOOP AT lt_bapiret2 INTO ls_bapiret2 WHERE type CA 'AIE'.
        lv_x = 'X'.
        s_return-zmessg = s_return-zmessg && '||' && ls_bapiret2-message.
      ENDLOOP.

      IF lv_x IS INITIAL.
        CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
          EXPORTING
            wait = 'X'.

      ELSE.
        CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
      ENDIF.

*&---------------------------------------------------------------------*
*&---------------------- 修改 ------------------------*
*&---------------------------------------------------------------------*

      CALL FUNCTION 'BAPI_BUPA_ROLE_ADD_2'     "财务视图-公司代码
        EXPORTING
          businesspartner             = ls_data_in-partner
          businesspartnerrolecategory = 'FLCU00'    "BP 角色类别  BU_PARTNERROLECAT
          businesspartnerrole         = 'FLCU00'    "业务伙伴角色 BU_PARTNERROLE
        TABLES
          return                      = lt_bapiret2.

      CLEAR:lv_x.
      LOOP AT lt_bapiret2 INTO ls_bapiret2 WHERE type CA 'AIE'.
        lv_x = 'X'.
        s_return-zmessg = s_return-zmessg && '||' && ls_bapiret2-message.
      ENDLOOP.

      IF lv_x IS INITIAL.
        CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
          EXPORTING
            wait = 'X'.

      ELSE.
        CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
      ENDIF.

**********************************************************************
*     结束









      CLEAR: lt_bapiret2.

      ls_data_in-zterm = ls_data_in-zterm."付款条件

****公司代码数据
      ls_header-object_instance-kunnr = ls_data_in-partner.
      ls_header-object_task = 'M'.
      ls_customer-header = ls_header.

      ls_company-task = 'M'.
      ls_company-data_key-bukrs = ls_data_in-bukrs.   "公司代码*
      ls_company-data-akont = ls_data_in-akont.         "统驭科目*
      ls_company-data-zuawa = ls_data_in-zuawa.  "排序码
      ls_company-data-fdgrv = ls_data_in-fdgrv."计划组
      ls_company-data-altkn = ls_data_in-altkn . " 先前主记录号码
      ls_company-data-zwels = ls_data_in-zwels .  " 付款方式
      ls_company-data-zterm = ls_data_in-zterm_f.        "付款条件
      ls_company-data-xzver = ls_data_in-xzver. "支付历史记录

      ls_company-datax-zterm = 'X'.
      ls_company-datax-fdgrv = 'X'.
      ls_company-datax-akont = 'X'.
      ls_company-datax-zwels = 'X'.
      ls_company-datax-zuawa = 'X'.
      ls_company-datax-fdgrv = 'X'.
      ls_company-datax-altkn = 'X'.
      ls_company-datax-zwels = 'X'.
      ls_company-datax-xzver = 'X'.

      APPEND ls_company TO lt_company.

      ls_customer-company_data-company = lt_company.  "公司数据

**通用数据-税收数据
      ls_customer-central_data-central-data-counc = ls_data_in-counc.
      ls_customer-central_data-central-data-cityc = ls_data_in-cityc.

      ls_customer-central_data-central-datax-counc = 'X'.
      ls_customer-central_data-central-datax-cityc = 'X'.

      ls_customer-central_data-central-data-ktokd = ls_data_in-bu_group.
      ls_customer-central_data-central-datax-ktokd = 'X'.


****销售数据
      ls_sales-task = 'M'.
      ls_sales-data_key-vkorg = ls_data_in-vkorg.             "销售机构*
      ls_sales-data_key-vtweg = ls_data_in-vtweg.
      ls_sales-data_key-spart = ls_data_in-spart.

      ls_sales-data-vsbed = ls_data_in-vsbed.                              "装运条件*
      ls_sales-data-vwerk = ls_data_in-vwerk.                  "交货工厂*
      ls_sales-data-ktgrd = ls_data_in-ktgrd.                  "客户科目分配组*  销售与分销-开票
      ls_sales-data-zterm = ls_data_in-zterm.                   "收付条件代码

      ls_sales-data-kalks = ls_data_in-kalks.                               "客户定价过程*  1
      ls_sales-data-waers = ls_data_in-waers.


      IF ls_data_in-bzirk IS NOT INITIAL.
        ls_sales-data-bzirk =  ls_data_in-bzirk.                           "销售地区--**
        ls_sales-data-kdgrp = ls_data_in-kdgrp.                  "客户组
        ls_sales-data-vkbur = ls_data_in-vkbur.                   "销售办事处*
        ls_sales-datax-bzirk =  'X'.
        ls_sales-datax-kdgrp =  'X'.
        ls_sales-datax-vkbur =  'X'.
      ENDIF.

      ls_sales-datax-vsbed =  'X'.
      ls_sales-datax-vwerk =  'X'.
      ls_sales-datax-ktgrd =  'X'.
      ls_sales-datax-zterm =  'X'.
      ls_sales-datax-vkgrp =  'X'.
      ls_sales-datax-kalks =  'X'.
      ls_sales-datax-waers =  'X'.

*SP.
      ls_function-task = 'M'.
      ls_function-data_key-parvw = 'AG'.
      ls_function-data_key-parza = 1.
      ls_function-data-partner = ls_data_in-partner.
      ls_function-datax-partner = 'X'.
      APPEND ls_function TO lt_function.
      ls_function-task = 'M'.
      ls_function-data_key-parvw = 'RE'.
      ls_function-data_key-parza = 2.
      ls_function-data-partner = ls_data_in-partner.
      ls_function-datax-partner = 'X'.
      APPEND ls_function TO lt_function.
      ls_function-task = 'M'.
      ls_function-data_key-parvw = 'RG'.
      ls_function-data_key-parza = 3.
      ls_function-data-partner = ls_data_in-partner.
      ls_function-datax-partner = 'X'.
      APPEND ls_function TO lt_function.
      ls_function-task = 'M'.
      ls_function-data_key-parvw = 'WE'.
      ls_function-data_key-parza = 4.
      ls_function-data-partner = ls_data_in-partner.
      ls_function-datax-partner = 'X'.
      APPEND ls_function TO lt_function.

      ls_sales-functions-functions = lt_function.

      APPEND ls_sales TO lt_sales.
      ls_customer-sales_data-sales = lt_sales .


*税收标识
      ls_tax-task = 'M'.
      ls_tax-data_key-aland = 'CN'.               "国家
      ls_tax-data_key-tatyp = 'MWST'.            "税收类别 (销项税)
      ls_tax-data-taxkd = ls_data_in-taxkd.                   "税分类*
      ls_tax-datax-taxkd = 'X'.
      APPEND ls_tax TO lt_tax.

      ls_customer-central_data-tax_ind-tax_ind = lt_tax.

*ls_mdata
      APPEND ls_customer TO lt_customer.

      ls_mdata-customers =  lt_customer.
*初始化
      CALL METHOD cmd_ei_api=>initialize.

*添加数据
      CALL METHOD cmd_ei_api=>maintain_bapi
        EXPORTING
          is_master_data           = ls_mdata
        IMPORTING
          es_master_data_correct   = es_master_data_correct
          es_message_correct       = es_msg_correct
          es_master_data_defective = es_master_data_defective
          es_message_defective     = es_msg_defective.

      IF es_msg_defective-is_error = 'X'.
        ROLLBACK WORK.
        lt_rete = es_msg_defective-messages.
        LOOP AT lt_rete INTO ls_rete.
          s_return-zmessg = '客户主数据创建成功，公司视图销售视图创建失败!' && '||' && ls_rete-message.
        ENDLOOP.
      ELSE.
        COMMIT WORK.
      ENDIF.
      CLEAR: ls_mdata.

    ELSEIF ls_data_in-zstatu = 'W' AND ls_data_in-zmessg = '||外部系统中的业务伙伴编号不唯一'.

      CLEAR:lt_return,lt_bapicomrem,lt_bapiadtel,g_partner.

      READ TABLE lt_but000 INTO DATA(ls_but000) WITH KEY bpext = ls_data_in-bpext BINARY SEARCH.
      IF sy-subrc = 0.
        g_partner = ls_but000-partner.
      ENDIF.


      ls_centraldata-searchterm1 = ls_data_in-bu_sort1.
      ls_centraldata_x-searchterm1 = 'X'.

      ls_organization-name1 = ls_data_in-name1.
      ls_organization_x-name1 = 'X'.


      CLEAR:ls_bapiadtel.
      ls_bapiadtel-country = ls_data_in-country. " 国家
      ls_bapiadtel-telephone = ls_data_in-tel_number.    "客户联系电话
      APPEND ls_bapiadtel TO lt_bapiadtel.

      CLEAR: ls_bapicomrem.
      ls_bapicomrem-comm_type = 'TEL'.
      ls_bapicomrem-comm_notes = ls_data_in-remark. " 名称
      ls_bapicomrem-langu = '1'.
      ls_bapicomrem-consnumber =  001.
      APPEND ls_bapicomrem TO lt_bapicomrem.

      ls_bapiadtelx-updateflag = 'U'.
      ls_bapiadtelx-country = 'X'.
      ls_bapiadtelx-telephone ='X'.
      APPEND ls_bapiadtelx TO lt_bapiadtelx.

      ls_bapicomrex-updateflag = 'U'.
      ls_bapicomrex-comm_type = 'X'.
      ls_bapicomrex-comm_notes = 'X'.
      ls_bapicomrex-langu = 'X'.
      ls_bapicomrex-consnumber = 'X'.
      APPEND ls_bapicomrex TO lt_bapicomrex.

      DATA:   ls_centraldatagroup TYPE bapibus1006_central_group.
      ls_centraldatagroup-grouptype = ls_data_in-bu_group.


      CALL FUNCTION 'BAPI_BUPA_CENTRAL_CHANGE'
        EXPORTING
          businesspartner               = g_partner
          centraldata                   = ls_centraldata
          centraldataorganization       = ls_organization
"         centraldatagroup              = ls_centraldatagroup
          centraldata_x                 = ls_centraldata_x
          centraldataorganization_x     = ls_organization_x
          valid_date                    = sy-datlo
          duplicate_check_address       = ls_address
        TABLES
          telefondatanonaddress         = lt_bapiadtel
          communicationnotesnonaddress  = lt_bapicomrem
          telefondatanonaddressx        = lt_bapiadtelx
          communicationnotesnonaddressx = lt_bapicomrex
          return                        = lt_return.
      CLEAR:lv_x.

      LOOP AT lt_return INTO ls_return WHERE type CA 'AIE'.
        lv_x = 'X'.
        s_return-zmessg = s_return-zmessg && '||' && ls_return-message.
      ENDLOOP.

      IF lv_x IS INITIAL.
        ls_data_in-partner = g_partner.

        CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
          EXPORTING
            wait = 'X'.
      ELSE.

        CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
      ENDIF.

      CLEAR: lt_return.

      ls_address-postl_cod1 = ls_data_in-post_code1.     "邮政编码*
      ls_address-city = ls_data_in-city1.           "城市*
      ls_address-country = ls_data_in-country.        "国家*
      ls_address-region = ls_data_in-region.          "地区
      ls_address-langu = sy-langu.                   "语言*

      "    ls_address_x-updateflag = 'U'.
      ls_address_x-postl_cod1 = 'X'.
      ls_address_x-city = 'X'.
      ls_address_x-country = 'X'.
      ls_address_x-region = 'X'.
      ls_address_x-langu = 'X'.

      CALL FUNCTION 'BAPI_BUPA_ADDRESS_CHANGE'
        EXPORTING
          businesspartner = ls_data_in-partner
          addressdata     = ls_address
          addressdata_x   = ls_address_x
        TABLES
          bapiadtel       = lt_bapiadtel
          bapicomrem      = lt_bapicomrem
          bapiadtel_x     = lt_bapiadtelx
          bapicomre_x     = lt_bapicomrex
          return          = lt_return.

      LOOP AT lt_return INTO ls_return WHERE type CA 'AIE'.
        lv_x = 'X'.
        s_return-zmessg = '客户主数据修改成功，地址修改失败' && '||' && ls_return-message.
      ENDLOOP.

      IF lv_x IS INITIAL.
        ls_data_in-partner = g_partner.
        CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
          EXPORTING
            wait = 'X'.
      ELSE.

        CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
      ENDIF.



      SELECT SINGLE rltyp INTO @DATA(ls_rltyp_01) FROM but100 WHERE partner = @ls_data_in-partner AND rltyp = 'FLCU01'.
      IF sy-subrc <> 0.
        "财务视图   FLCU00
        CALL FUNCTION 'BAPI_BUPA_ROLE_ADD_2'     "财务视图-公司代码
          EXPORTING
            businesspartner             = ls_data_in-partner
            businesspartnerrolecategory = 'FLCU01'    "BP 角色类别  BU_PARTNERROLECAT
            businesspartnerrole         = 'FLCU01'    "业务伙伴角色 BU_PARTNERROLE
          TABLES
            return                      = lt_bapiret2.

        CLEAR:lv_x.
        LOOP AT lt_bapiret2 INTO ls_bapiret2 WHERE type CA 'AIE'.
          lv_x = 'X'.
          s_return-zmessg = s_return-zmessg && '||' && ls_bapiret2-message.
        ENDLOOP.

        IF lv_x IS INITIAL.
          CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
            EXPORTING
              wait = 'X'.

        ELSE.
          CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
        ENDIF.
      ENDIF.

*&---------------------------------------------------------------------*
*&---------------------- 修改 ------------------------*
*&---------------------------------------------------------------------*

      SELECT SINGLE rltyp INTO @DATA(ls_rltyp_00) FROM but100 WHERE partner = @ls_data_in-partner AND rltyp = 'FLCU00'.
      IF sy-subrc <> 0.
        CALL FUNCTION 'BAPI_BUPA_ROLE_ADD_2'     "财务视图-公司代码
          EXPORTING
            businesspartner             = ls_data_in-partner
            businesspartnerrolecategory = 'FLCU00'    "BP 角色类别  BU_PARTNERROLECAT
            businesspartnerrole         = 'FLCU00'    "业务伙伴角色 BU_PARTNERROLE
          TABLES
            return                      = lt_bapiret2.

        CLEAR:lv_x.
        LOOP AT lt_bapiret2 INTO ls_bapiret2 WHERE type CA 'AIE'.
          lv_x = 'X'.
          s_return-zmessg = s_return-zmessg && '||' && ls_bapiret2-message.
        ENDLOOP.

        IF lv_x IS INITIAL.
          CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
            EXPORTING
              wait = 'X'.
        ELSE.
          CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
        ENDIF.
      ENDIF.
***********************************************************************
**     结束

      SELECT SINGLE partner INTO @DATA(lv_partner)  FROM but0bk WHERE partner = @ls_data_in-partner.
      IF sy-subrc = 0.
*****添加银行*******         通用数据-支付交易
        ls_bank-bank_ctry = 'CN'. "国家代码
        ls_bank-bank_key = ls_data_in-bankl. "银行代码
        ls_bank-bank_acct = ls_data_in-bankn(18).

        ls_bankx-bank_ctry = 'X'.
        ls_bankx-updateflag = 'U'.
        ls_bankx-bank_key = 'X'.
        ls_bankx-bank_acct = 'X'.


        CALL FUNCTION 'BAPI_BUPA_BANKDETAIL_CHANGE'
          EXPORTING
            businesspartner  = ls_data_in-partner
            bankdetailid     = '0001'
            bankdetaildata   = ls_bank
            bankdetaildata_x = ls_bankx
          TABLES
            return           = lt_re.

        CLEAR:lv_x.
        LOOP AT lt_re INTO ls_re WHERE type CA 'AIE'.
          lv_x = 'X'.
          s_return-zmessg = '客户主数据修改成功，银行数据错误' && s_return-zmessg && '||' && ls_re-message.
        ENDLOOP.

        IF lv_x IS NOT INITIAL.
          CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
        ELSE.
          CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
            EXPORTING
              wait = 'X'.
        ENDIF.
        CLEAR :ls_kna1,lt_re.

      ELSE.

*****添加银行*******         通用数据-支付交易
        ls_bank-bank_ctry = 'CN'. "国家代码
        ls_bank-bank_key = ls_data_in-bankl. "银行代码
        ls_bank-bank_acct = ls_data_in-bankn(18).

        CALL FUNCTION 'BAPI_BUPA_BANKDETAIL_ADD'
          EXPORTING
            businesspartner = ls_data_in-partner
            bankdetailid    = '0001'
            bankdetaildata  = ls_bank
*         IMPORTING
*           BANKDETAILIDOUT =
          TABLES
            return          = lt_re.

        CLEAR:lv_x.
        LOOP AT lt_re INTO ls_re WHERE type CA 'AIE'.
          lv_x = 'X'.
          s_return-zmessg = '客户主数据修改成功，银行数据错误' && s_return-zmessg && '||' && ls_re-message.
        ENDLOOP.

        IF lv_x IS NOT INITIAL.
          CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
        ELSE.
          CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
            EXPORTING
              wait = 'X'.
        ENDIF.
        CLEAR :ls_kna1,lt_re.

      ENDIF.







      ls_data_in-zterm = ls_data_in-zterm."付款条件

****公司代码数据
      ls_header-object_instance-kunnr = ls_data_in-partner.
      ls_header-object_task = 'M'.
      ls_customer-header = ls_header.

      ls_company-task = 'M'.
      ls_company-data_key-bukrs = ls_data_in-bukrs.   "公司代码*
      ls_company-data-akont = ls_data_in-akont.         "统驭科目*
      ls_company-data-zuawa = ls_data_in-zuawa.  "排序码
      ls_company-data-fdgrv = ls_data_in-fdgrv."计划组
      ls_company-data-altkn = ls_data_in-altkn . " 先前主记录号码
      ls_company-data-zwels = ls_data_in-zwels .  " 付款方式
      ls_company-data-zterm = ls_data_in-zterm_f.        "付款条件
      ls_company-data-xzver = ls_data_in-xzver. "支付历史记录

      ls_company-datax-zterm = 'X'.
      ls_company-datax-fdgrv = 'X'.
      ls_company-datax-akont = 'X'.
      ls_company-datax-zwels = 'X'.
      ls_company-datax-zuawa = 'X'.
      ls_company-datax-fdgrv = 'X'.
      ls_company-datax-altkn = 'X'.
      ls_company-datax-zwels = 'X'.
      ls_company-datax-xzver = 'X'.

      APPEND ls_company TO lt_company.

      ls_customer-company_data-company = lt_company.  "公司数据

**通用数据-税收数据
      ls_customer-central_data-central-data-counc = ls_data_in-counc.
      ls_customer-central_data-central-data-cityc = ls_data_in-cityc.

      ls_customer-central_data-central-datax-counc = 'X'.
      ls_customer-central_data-central-datax-cityc = 'X'.

      ls_customer-central_data-central-data-ktokd = ls_data_in-bu_group.
      ls_customer-central_data-central-datax-ktokd = 'X'.


****销售数据
      ls_sales-task = 'M'.
      ls_sales-data_key-vkorg = ls_data_in-vkorg.             "销售机构*
      ls_sales-data_key-vtweg = ls_data_in-vtweg.
      ls_sales-data_key-spart = ls_data_in-spart.

      ls_sales-data-vsbed = ls_data_in-vsbed.                              "装运条件*
      ls_sales-data-vwerk = ls_data_in-vwerk.                  "交货工厂*
      ls_sales-data-ktgrd = ls_data_in-ktgrd.                  "客户科目分配组*  销售与分销-开票
      ls_sales-data-zterm = ls_data_in-zterm.                   "收付条件代码

      ls_sales-data-kalks = ls_data_in-kalks.                               "客户定价过程*  1
      ls_sales-data-waers = ls_data_in-waers.


      IF ls_data_in-bzirk IS NOT INITIAL.
        ls_sales-data-bzirk =  ls_data_in-bzirk.                           "销售地区--**
        ls_sales-data-kdgrp = ls_data_in-kdgrp.                  "客户组
        ls_sales-data-vkbur = ls_data_in-vkbur.                   "销售办事处*
        ls_sales-datax-bzirk =  'X'.
        ls_sales-datax-kdgrp =  'X'.
        ls_sales-datax-vkbur =  'X'.
      ENDIF.

      ls_sales-datax-vsbed =  'X'.
      ls_sales-datax-vwerk =  'X'.
      ls_sales-datax-ktgrd =  'X'.
      ls_sales-datax-zterm =  'X'.
      ls_sales-datax-vkgrp =  'X'.
      ls_sales-datax-kalks =  'X'.
      ls_sales-datax-waers =  'X'.

*SP.knvp

      SELECT SINGLE parvw INTO @DATA(lv_parvw) FROM knvp WHERE kunnr = @ls_data_in-partner.
      IF sy-subrc <> 0.
        ls_function-task = 'M'.
        ls_function-data_key-parvw = 'AG'.
        ls_function-data_key-parza = 1.
        ls_function-data-partner = ls_data_in-partner.
        ls_function-datax-partner = 'X'.
        APPEND ls_function TO lt_function.
        ls_function-task = 'M'.
        ls_function-data_key-parvw = 'RE'.
        ls_function-data_key-parza = 2.
        ls_function-data-partner = ls_data_in-partner.
        ls_function-datax-partner = 'X'.
        APPEND ls_function TO lt_function.
        ls_function-task = 'M'.
        ls_function-data_key-parvw = 'RG'.
        ls_function-data_key-parza = 3.
        ls_function-data-partner = ls_data_in-partner.
        ls_function-datax-partner = 'X'.
        APPEND ls_function TO lt_function.
        ls_function-task = 'M'.
        ls_function-data_key-parvw = 'WE'.
        ls_function-data_key-parza = 4.
        ls_function-data-partner = ls_data_in-partner.
        ls_function-datax-partner = 'X'.
        APPEND ls_function TO lt_function.
        ls_sales-functions-functions = lt_function.
      ENDIF.


      APPEND ls_sales TO lt_sales.
      ls_customer-sales_data-sales = lt_sales .


*税收标识
      ls_tax-task = 'M'.
      ls_tax-data_key-aland = 'CN'.               "国家
      ls_tax-data_key-tatyp = 'MWST'.            "税收类别 (销项税)
      ls_tax-data-taxkd = ls_data_in-taxkd.                   "税分类*
      ls_tax-datax-taxkd = 'X'.
      APPEND ls_tax TO lt_tax.

      ls_customer-central_data-tax_ind-tax_ind = lt_tax.

*ls_mdata
      APPEND ls_customer TO lt_customer.

      ls_mdata-customers =  lt_customer.
*初始化
      CALL METHOD cmd_ei_api=>initialize.

*添加数据
      CALL METHOD cmd_ei_api=>maintain_bapi
        EXPORTING
          is_master_data           = ls_mdata
        IMPORTING
          es_master_data_correct   = es_master_data_correct
          es_message_correct       = es_msg_correct
          es_master_data_defective = es_master_data_defective
          es_message_defective     = es_msg_defective.

      IF es_msg_defective-is_error = 'X'.
        ROLLBACK WORK.
        lt_rete = es_msg_defective-messages.
        LOOP AT lt_rete INTO ls_rete.
          s_return-zmessg = '客户主数据创建成功，公司视图销售视图创建失败!' && '||' && ls_rete-message.
        ENDLOOP.
      ELSE.
        COMMIT WORK.
      ENDIF.
      CLEAR: ls_mdata.

    ENDIF.

********RETURN********
    IF ls_data_in-partner IS NOT INITIAL AND s_return-zmessg IS  INITIAL.
      s_return-partner = ls_data_in-partner.
      s_return-zstatu = 'S'.
    ELSE.
      s_return-partner = ls_data_in-partner.
      s_return-zstatu = 'E'.
    ENDIF.
*    s_return-ersda = sy-datum.
*    s_return-created_at_time = sy-uzeit.
    APPEND s_return TO et_return.
    CLEAR: s_return,ls_data_in.

  ENDLOOP.




  LOOP GT_OUT INTO GS_OUT.

    IF GS_OUT-MATNR = A.
      

    ENDIF.

  ENDLOOP.

ENDFUNCTION.